package zh.demo.webuser.service.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zh.demo.commons.exception.RRException;
import zh.demo.commons.validator.Assert;
import zh.demo.webuser.dao.WebUserDao;
import zh.demo.webuser.model.UserEntity;
import zh.demo.webuser.service.WebUserService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("webUserService")
public class WebUserServiceImpl implements WebUserService {
	@Autowired
	private WebUserDao userDao;
	
	@Override
	public UserEntity queryObject(Long userId){
		return userDao.queryObject(userId);
	}
	
	@Override
	public List<UserEntity> queryList(Map<String, Object> map){
		return userDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return userDao.queryTotal(map);
	}
	
	@Override
	public void save(UserEntity user){
		userDao.save(user);
	}

	@Override
	public void save(String mobile, String password){
		UserEntity user = new UserEntity();
		user.setMobile(mobile);
		user.setUsername(mobile);
		user.setPassword(DigestUtils.sha256Hex(password));
		user.setCreateTime(new Date());
		userDao.save(user);
	}

	@Override
	public void update(UserEntity user){
		userDao.update(user);
	}
	
	@Override
	public void delete(Long userId){
		userDao.delete(userId);
	}
	
	@Override
	public void deleteBatch(Long[] userIds){
		userDao.deleteBatch(userIds);
	}

	@Override
	public UserEntity queryByMobile(String mobile) {
		return userDao.queryByMobile(mobile);
	}

	@Override
	public long login(String mobile, String password) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mobile",mobile);
		//SysUserEntity user = sysUserDao.queryUser(map);
		UserEntity user = queryByMobile(mobile);
		Assert.isNull(user, "手机号或密码错误");

		//账号不存在、密码错误
		if(user == null || !user.getPassword().equals(new Sha256Hash(password, user.getSalt()).toHex())) {
			throw new RRException("账号或密码不正确");
		}

		//账号锁定
		if(user.getStatus() == 0){
			throw new RRException("账号已被锁定,请联系管理员");
		}

		/*//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(password))){
			throw new RRException("手机号或密码错误");
		}*/

		return user.getUserId();
	}

	@Override
	public void thirdPartAccountRegister(String account, String type) {
		UserEntity user = new UserEntity();
		user.setCreateTime(new Date());
		//sha256加密
		String salt = RandomStringUtils.randomAlphanumeric(20);
		//user.setPassword(new Sha256Hash("123456", salt).toHex());
		//user.setSalt(salt);
		user.setThirdPartAccount(account);
		user.setThirdPartType(type);
		userDao.saveByDynamicCol(user);//save(user);

		//检查角色是否越权
		/*checkRole(user);

		//保存用户与角色关系
		List<Long> list = new ArrayList<>();
		list.add(1L);
		sysUserRoleService.saveOrUpdate(user.getUserId(), list);*/
	}

	@Override
	public UserEntity thirdPartAccountLogin(String account, String type) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("thirdparttype",type);
		map.put("thirdpartaccount",account);
		return userDao.queryByThirdPartAccount(map);
	}
	
}
